﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Windows;
using SongSharp.Presentation.View;
using SongSharp.Presentation.Command;
using System.Windows.Input;
using SongSharp.Logging;

namespace SongSharp.Presentation.ViewModel
{
    public class LogViewModel : ViewModelBase
    {
        private string m_logViewMessages;
        private LogView LogView { get; set; }
        private DelegateCommand m_clearLogCommand;
        private Logger.LogInfo m_logInfo = Logger.LogInfo.Instance;
        private Window Owner { get; set; }

        public LogViewModel(Window owner)
        {
            Owner = owner;
            m_logViewMessages = "";
            m_logInfo.PropertyChanged += new PropertyChangedEventHandler(LogInfo_PropertyChanged);
        }

        void LogInfo_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            switch (e.PropertyName)
            {
                case "Log":
                    lock (m_logInfo)
                    {
                        if (m_logInfo.Log != string.Empty)
                        {

                            LogViewMessages += m_logInfo.Log;
                            m_logInfo.Log = "";
                        }

                        break;
                    }
            }
        }

        public void ShowWindow()
        {
            if (LogView == null || !LogView.IsLoaded)
            {
                LogView = new LogView();
                LogView.DataContext = this;
                LogView.Owner = Owner;
                LogView.Show();
            }
            else
            {
                LogView.Activate();
            }
        }

        public string LogViewMessages
        {
            get { return m_logViewMessages; }
            set
            {
                m_logViewMessages = value;
                OnPropertyChanged("LogViewMessages");
            }
        }

        public ICommand ClearLogCommand
        {
            get
            {
                if (m_clearLogCommand == null)
                    m_clearLogCommand = new DelegateCommand(ClearLog);
                return m_clearLogCommand;
            }
        }

        private void ClearLog()
        {
            LogViewMessages = "";
        }
    }
}
